package org.jslaughter.component.effect;

import org.jslaughter.event.eventtype.DamageEvent;
import org.jslaughter.event.eventtype.DamageListener;
import org.nvframe.component.AbstractComponent;
import org.nvframe.component.state.Position;
import org.nvframe.entity.Entity;
import org.nvframe.event.EventService;
import org.nvframe.exception.NVFrameException;
import org.nvframe.factory.EntityFactory;
import org.nvframe.util.Settings;

/**
 * 
 * @author Nik Van Looy
 */
public class BloodOnDamage extends AbstractComponent implements DamageListener {
	
	private String bloodEntity;

	public BloodOnDamage(String id, Entity owner, Settings settings) {
		super(id, owner);
		
		bloodEntity = settings.getString("bloodEntity");
		
		EventService.getInstance().addEventListener(this);
	}
	
	@Override
	public void damageReceived(DamageEvent event) {
		if(event.getTarget() == owner) {
			try {
				Entity entBlood = EntityFactory.getInstance().getEntityFromPrototype(bloodEntity);
				Position position = (Position) owner.getComponent(Position.class);
				Position bloodPosition = (Position) entBlood.getComponent(Position.class);
				
				bloodPosition.setXY(position.getXY());
				bloodPosition.setDirection((Math.round(Math.random() * 359) + 1));
			} catch (NVFrameException e) {
				e.printStackTrace();
			}
		}
	}
	
	@Override
	public void removed() {
		EventService.getInstance().removeEventListener(this);
	}
}
